package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.StoreShopSearchEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class StoreShopSearchItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<String> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public BaseActivity context;

   public StoreShopSearchItemListAdapter(BaseActivity context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_store_shop_search, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
                   IndexHolder ih= (IndexHolder) viewHolder;
                  String data=dataBeanList.get(position);
              ih.title.setText(data);

              ih.rootView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      StoreShopSearchEvent event=new StoreShopSearchEvent();
                      event.flag=StoreShopSearchEvent.Search_Checked;
                      event.data=data;
                      EventBus.getDefault().post(event);
                  }
              });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<String> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<String> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void delete(int position){
        dataBeanList.remove(position);
        notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }





   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
         public TextView title;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            title=itemView.findViewById(R.id.title);

        }
   }



}